package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.auth.model.BaseUser;
import com.redoor.app.stake.auth.service.BaseUserService;
import com.redoor.app.stake.basicdata.dao.BaseChargerMapper;
import com.redoor.app.stake.basicdata.model.*;
import com.redoor.app.stake.basicdata.model.BaseChargerCriteria;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.model.serviceModel.BaseChargerServiceBean;
import com.redoor.app.stake.basicdata.mq.sendService.impl.SendMQServiceImpl;
import com.redoor.app.stake.basicdata.service.*;

import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoornetwork.framework.mybatis.service.AbstractService;
import com.yotouch.app.stake.model.Charger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by wangjunlei on 2018-02-12 10:08:10.
 */
@Service
public class BaseChargerServiceImpl implements BaseChargerService {

    private Logger logger = LoggerFactory.getLogger(BaseChargerServiceImpl.class);

    @Autowired
    private BaseChargerMapper baseChargerMapper;

    @Autowired
    private BaseChargerRedisService baseChargerRedisService;


    @Autowired
    private SendMQServiceImpl mqService;

    @Autowired
    private BaseAssetOperationTrajectoryService baseAssetOperationTrajectoryService;

    @Autowired
    private BaseSystemOperationLogService baseSystemOperationLogService;

    @Autowired
    private BaseUserService baseUserService;



    @Override
    public List<BaseChargerServiceBean> queryChargers(BaseChargerCriteria criteria) {

        List<BaseChargerServiceBean> chargers = baseChargerMapper.queryChargers(criteria);

        return chargers;
    }

    @Override
    public List<BaseChargerServiceBean> queryChargersByStakeUuid(String stakeUuid) {

        BaseChargerCriteria criteria = new BaseChargerCriteria();
        criteria.setStakeUuid(stakeUuid);

        List<BaseChargerServiceBean> chargers = baseChargerMapper.queryChargers(criteria);

        return chargers;
    }

    @Override
    public BaseCharger getChargerByUuid(String uuid) {
        BaseCharger charger = null;
        charger = baseChargerRedisService.getChargerFromRedis(uuid);
        if (charger==null){
            charger = baseChargerMapper.getChargerByUuid(uuid);
        }
        return charger;
    }
    @Override
    public int updateCharger(BaseCharger charger) {
        if(charger==null){
            logger.info("修改充电枪时，枪对象不能为空!");
            return 0;
        }
        BaseCharger oldCharger = baseChargerMapper.getChargerByUuid(charger.getUuid());
        //  异常数据的修改 TODO
        // 修改原来stakeNo比较为stakeUuid比较 前端获取stakeNo具有不确定性 zhangYiHang begin 2018.4.13
        String stakeNo = oldCharger.getStakeNo();
        BaseCharger baseCharger = new BaseCharger();
        baseCharger.setStakeNo(stakeNo);
        baseCharger.setStatus(1000);
        List<BaseCharger> list = this.baseChargerMapper.select(baseCharger);
        if (list.size() > 1){
          // end
            charger.setAssetNo(stakeNo + (int)(Math.random()*100)+"1");
            charger.setOutchargerno(stakeNo + (int)(Math.random()*100)+"1");
        }
        // end
        charger.setUpdatedat(new Date()); //数据更新时间
        //int res = baseChargerMapper.updateCharger(charger);
        int res = baseChargerMapper.updateByPrimaryKeySelective(charger);

        if (res >= 1){
            boolean b = baseChargerRedisService.modifyChargerInfo(charger.getUuid(),charger);
            if (!b){
                logger.info("更新枪信息时，相应redis数据更新失败!");
            }

            //添加操作轨迹
            baseAssetOperationTrajectoryService.addTraceLog(charger.getUuid(),"UPDATE", BussinessTypeEnum.TRACE_BUSINESS_TYPE.CHARGE,BussinessTypeEnum.TRACE_TYPE.WEB,"",oldCharger,charger);
            //枪修改操作日志
            baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.CHARGE, BussinessTypeEnum.TAG_TYPE.UPDATE,charger.getUuid(),"",true);
        }

        return res;
    }

    @Override
    public int insertCharger(BaseCharger charger) {

        if(charger==null){
            return 0;
        }
        if (charger.getUuid()==null || charger.getUuid().trim().length()==0){
            charger.setUuid(UUIDTool.getUUID());
            charger.setCreatedat(new Date());
            charger.setUpdatedat(new Date());
        }

        if (charger.getStatus()==null) {
            charger.setStatus(1000);
        }

        BaseUser loginUser = this.baseUserService.getShiroUser(null);
        charger.setCreatoruuid(loginUser.getUuid());

        if (charger.getAssetNo()==null || charger.getAssetNo().trim().length()==0) {
            charger.setAssetNo(charger.getStakeNo() + "001");
            charger.setOutchargerno(charger.getStakeNo() + "001");
            charger.setChargerno("0");
        }

//        int res = baseChargerMapper.insertCharger(charger);
        int res = baseChargerMapper.insert(charger);
        if (res>=1) {
            boolean b = baseChargerRedisService.modifyChargerInfo(charger.getUuid(), charger);
            if (!b){
                logger.info("创建枪数据，保存数据到redis失败!");
            }

            //添加操作轨迹
            baseAssetOperationTrajectoryService.addTraceLog(charger.getUuid(),"CREATE",BussinessTypeEnum.TRACE_BUSINESS_TYPE.CHARGE,BussinessTypeEnum.TRACE_TYPE.WEB,"",null,charger);
            //创建枪操作日志
            baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.CHARGE, BussinessTypeEnum.TAG_TYPE.ADD,charger.getUuid(),"",true);
        }
        return res;
    }

    @Override
    public int deleteChargerByUuid(String uuid) {

        BaseCharger charger = baseChargerMapper.getChargerByUuid(uuid);

        int res = baseChargerMapper.deleteChargerByUuid(uuid);

        if (res >= 1){
            //根据uuid删除redis缓存中的枪数据
            baseChargerRedisService.deleteChargerInfo(uuid);

            //添加操作轨迹
            baseAssetOperationTrajectoryService.addTraceLog(charger.getUuid(),"DELETE",BussinessTypeEnum.TRACE_BUSINESS_TYPE.CHARGE,BussinessTypeEnum.TRACE_TYPE.WEB,"",null,charger);
            //枪删除操作日志
            baseSystemOperationLogService.addSystemOperationLog(BussinessTypeEnum.WEB_OPERATOR_TYPE.CHARGE, BussinessTypeEnum.TAG_TYPE.DELETE,charger.getUuid(),"",true);
        }

        return res;
    }

    @Override
    public int sgccTotalCount() {

        BaseChargerCriteria criteria = new BaseChargerCriteria();
        criteria.setIsThirds(BasicDataRedisConstants.THIRD_SGCC);//国网
        int count = baseChargerMapper.sgccTotalCount(criteria);

        return count;
    }

    @Override
    public List<Charger> sgccCharger(int offset, int limit) {

        BaseChargerCriteria criteria = new BaseChargerCriteria();

        criteria.setIsThirds(BasicDataRedisConstants.THIRD_SGCC);//国网
        criteria.setLimit(limit);
        criteria.setOffset(offset);

        return baseChargerMapper.sgccCharger(criteria);
    }

    @Override
    public Charger getChargerHsf(String uuid) {

        Charger charger = null;

        charger = baseChargerRedisService.getChargerHSF(uuid); // 先从缓存查询数据
        if (charger==null) {
            BaseChargerCriteria criteria = new BaseChargerCriteria();
            criteria.setUuid(uuid);
            List<Charger> chargers = baseChargerMapper.sgccCharger(criteria);
            charger = (chargers != null && chargers.size() > 0) ? chargers.get(0) : null;

            if (charger!=null){
                baseChargerRedisService.modifyChargerInfoHSF(charger.getUuid(),charger);//将数据缓存到redis
            }
        }
        return charger;
    }

    @Override
    public Charger getChargerHsf(String stakeNo, String chargerNo) {
        Charger charger = null;

        String uuid = baseChargerRedisService.getUuidByChargerNo(chargerNo);// 从缓存查找主键
        if (uuid!=null){
            charger = baseChargerRedisService.getChargerHSF(uuid); //根据主键查找枪数据
        }

        if (charger==null) {
            BaseChargerCriteria criteria = new BaseChargerCriteria();
            criteria.setStakeNo(stakeNo);
            criteria.setChargerNo(chargerNo);
            List<Charger> chargers = baseChargerMapper.sgccCharger(criteria);
            charger = (chargers != null && chargers.size() > 0) ? chargers.get(0) : null;

            if (charger!=null){
                baseChargerRedisService.modifyChargerInfoHSF(charger.getUuid(),charger);//将数据缓存到redis
            }
        }

        return charger;
    }

    @Override
    public List<Charger> listByParkingArea(String parkingAreaUuid) {

        BaseChargerCriteria criteria = new BaseChargerCriteria();

        List<Integer> isThirds = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4));// 国网和非国网
        List<Integer> stakestatus = new ArrayList<Integer>(Arrays.asList(3));// 运行状态

        criteria.setIsThirds(isThirds);
        criteria.setStakestatus(stakestatus);
        criteria.setParkingAreaUuid(parkingAreaUuid);

        return baseChargerMapper.sgccCharger(criteria);
    }

    @Override
    public List<Charger> listByStakeNo(String stakeNo) {

        BaseChargerCriteria criteria = new BaseChargerCriteria();
        criteria.setStakeNo(stakeNo);

        List<Charger> chargers = baseChargerMapper.sgccCharger(criteria);

        return chargers;
    }

    @Override
    public int queryChargersTotal(BaseChargerCriteria criteria) {
        return baseChargerMapper.queryChargersTotal(criteria);
    }

    @Override
    public List<Charger> hsfChargers(BaseChargerCriteria criteria) {
        return baseChargerMapper.sgccCharger(criteria);
//        return baseChargerMapper.hsfChargers(criteria);
    }

    @Override
    public int hsfChargersCount(BaseChargerCriteria criteria) {
        return baseChargerMapper.hsfChargersCount(criteria);
    }


}
